package com.basker.pisces.domain.value;

import org.springframework.util.StringUtils;

public class StringValue implements IValueObject {
    public static final StringValue EMPTY = new EmptyStringValue();

    private String value;

    public StringValue(Object value) {
        this.setValue(value);
    }

    protected StringValue() {

    }

    @Override
    public Object getDataValue() {
        return value;
    }

    @Override
    public String getPresentValue() {
        return this.isEmpty() ? "" : value;
    }

    @Override
    public boolean isEmpty() {
        return StringUtils.isEmpty(value);
    }

    @Override
    public String toString() {
        return String.valueOf(this.getPresentValue());
    }

    protected void setValue(Object value) {
        if (value instanceof String) {
            this.value = ((String) value).trim();
        } else if (value != null) {
            this.value = value.toString().trim();
        }
    }

    private final static class EmptyStringValue extends StringValue {

        @Override
        public Object getDataValue() {
            return null;
        }

        @Override
        public String getPresentValue() {
            return "";
        }

    }
}
